import { isInBounds, shanghua, sleepRandom1, sleepRandom3 } from "./untils";


auto();
let currentControl='aabbcc';

sleep(1000);
for(let i =0;i<200;i++){
    const c = new Date().getTime();
    const r = 1726412292000>c;
    if(r){
        once();
    }
}
  


function once(){
     const listItemPos = guanzhu(); // 点击关注，返回这个已关注按钮的坐标 
     if(listItemPos){
        sleepRandom3();
        currentControl =selector().desc("已关注按钮").visibleToUser(true).findOnce().parent().children()[0].desc();
        click(device.width/2,listItemPos.centerY()) // 点击列表项
        sleepRandom3();
        const canContinue = zuoping(); // 找到视频点击视频
        if(canContinue){
            sleepRandom3();
            pinglun();
            sleepRandom3();
        }
       
     }
   
}

// 判断页面是否有已关注按钮
function hasGuanzhu(){
    return !!selector().desc("已关注按钮").visibleToUser(true).findOnce();
}

// 判断是否是当前这个列
function judgeIsCurrentList(){
     const currentControl2 = selector().desc("已关注按钮").visibleToUser(true).findOnce().parent().children()[0].desc();
     return currentControl2 === currentControl
   
}

// 判断关注是否在list中
function judgeGuanzhuInList(){
    
    const guanzhuBtn = selector().desc("已关注按钮").visibleToUser(true).findOnce();
    if(!guanzhuBtn){
        return false;
    }
    let guanzhuBtnPos = guanzhuBtn.bounds();
      // 找到列表区域的范围
    const listArea = selector().id("com.ss.android.ugc.aweme:id/2kx").findOnce();
    if(!listArea){
        return false;
    }
    const listAreaBounds=listArea.bounds();
    // 查看已关注按钮在不在列表区域内
    let isContainer = isInBounds(listAreaBounds,guanzhuBtnPos);
    return isContainer;
}

function guanzhu(){
   
    while(!(hasGuanzhu()&&judgeGuanzhuInList()&&!judgeIsCurrentList())){
        shanghua(50);
        sleepRandom1();
        console.log('循环中的描述',currentControl);
    }

    const guanzhuBtn = selector().desc("已关注按钮").visibleToUser(true).findOnce()
    if(guanzhuBtn){
        const guanzhuBtnPos = guanzhuBtn.bounds();
        // click(guanzhuBtnPos.centerX(),guanzhuBtnPos.bottom);
        return guanzhuBtnPos;
    }else{
        log('没有已关注按钮');
        return null;
    }
    

}

function zuoping(){
    let zuopingIcon = selector().descStartsWith('作品').textStartsWith('作品').findOnce();
    // 没有作品
    if(!zuopingIcon){
        sleepRandom1();
        back();
        sleepRandom1();
        return null;
    }
    const tabLength = selector().className("androidx.appcompat.app.ActionBar$Tab").find().size();
    // 只有作品一个tab,什么都不做
    if(tabLength === 1){

    }else{
        if(!zuopingIcon.selected()){
            let num = zuopingIcon.bounds();
            click(num.centerX(),num.centerY());
            sleepRandom3();
        }
    }

    // 判断是否有视频
   let hasVideo = selector().id("com.ss.android.ugc.aweme:id/on5").visibleToUser(true).find();
   if(hasVideo.size()===0){
    back();
    sleepRandom1();
    return null;
   }else{

    const huadongTimes = random(1, 10);
    // 随机滑动完成
    for(let i =0;i<huadongTimes;i++){
        const r1 = Math.floor(random(100, 1000));
        shanghua(r1); 
        sleepRandom1();
    }

    // 滑动完成随机点击
    const t = selector().id("com.ss.android.ugc.aweme:id/on5").visibleToUser(true).find(); // 找到所有点赞数
    const index = Math.floor(Math.random() * t.size()); // 随机取一个点赞数
    t[index].click();
    return true;
   }

}

function pinglun(){
    let phrases = ['厉害了[强]', '专业[强]', '老板大吉大利', '老板恭喜发财','爱评论的女孩运气不会太差[比心]'];
    let wenan = getRandomString(phrases);
    console.log(wenan);
    const pinglunBtn =  selector().descStartsWith('评论').visibleToUser(true).findOnce();
    pinglunBtn.click(); // 找到最右边的评论按钮
    sleepRandom3();
    let input = selector().id(("com.ss.android.ugc.aweme:id/dik")).visibleToUser(true).findOnce();
    input.click();
    sleepRandom1();
    setText(wenan); 
    sleepRandom1();
    back();
    sleepRandom3();
    const fasong = selector().id('com.ss.android.ugc.aweme:id/dl6').findOnce();
    if(fasong){
        sleepRandom3();
        fasong.click();
        sleepRandom3();
    }
    
    back();
    sleepRandom3();
    const fasong2 = selector().id("com.ss.android.ugc.aweme:id/dl6").findOnce();
    console.log(fasong2);
    if(fasong2){
        sleepRandom3();
        fasong2.click();
        sleepRandom3();
    }
    sleepRandom3();
    back();
    sleepRandom3();
    back();
    sleepRandom3();
}


function getRandomString(array) {
    // 生成一个 0 到 array.length - 1 之间的随机整数
    let randomIndex = Math.floor(Math.random() * array.length);
    // 返回随机索引对应的数组元素
    return array[randomIndex];
}
